﻿package 
{
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	import fl.transitions.Fade;
	
	public class Overworld extends MovieClip 
	{
		public var Player:OverworldPlayer;
		
		private var OverworldBackgroundName:String;
		private var OverworldBackground:Background;
		
		private var FaderClip:Fader;
		
		public static const PlayerClipName = "PlayerClipName";
		private static const MessageClipName = "MessageClipName";
		
		public var MessageIsShown:Boolean;
		private var RemoveMessage:Boolean;
		private var MakingDecion:Boolean;
		private var Countdown:int;
		
		private var MessageQueue:String;
		private var CommandName:String;
		
		private var frameToPlay:int
		
		public function Overworld(instanceName:String, xPos:int, yPos:int)
		{
			init();
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			addEventListener(Event.REMOVED, Unload);
			
			AddContent(instanceName, xPos, yPos);
		}
		
		private function init():void
		{
			MessageIsShown = false;
			RemoveMessage = false;
			MakingDecion = false;
			
			MessageQueue = StringHelper.Empty;
		}
		
		private function Unload(event:Event):void
		{
			if (event.target.name == this.name)
			{
				removeChildren();
				removeEventListener(Event.ENTER_FRAME, EnterFrame);
			}
		}
		
		private function EnterFrame(event:Event):void
		{
			if (OverworldBackground != null && OverworldBackground.RandomBattles && Countdown <= 0)
			{
				if (!FaderClip.PlayingFader)
				{
					MessageIsShown = true;
					playFader(100, Number("0xFFFFFF"), .05);
					if (Player.body != null)
					{
						Player.body.gotoAndStop(1);
					}
				}
				else if (FaderClip.CompletedTransition)
				{
					goToBattle();
				}
			}
			
			if (RemoveMessage)
			{
				DeleteMessage();
			}
			if (!MessageIsShown)
			{
				if (Game.UpIsDown || Game.DownIsDown || Game.LeftIsDown || Game.RightIsDown)
				{
					Player.CaptureXY();

					if (Game.UpIsDown)
					{
						frameToPlay = 2;
					}
					else if (Game.DownIsDown)
					{
						frameToPlay = 1;
					}
					else if (Game.LeftIsDown)
					{
						frameToPlay = 4;
					}
					else if (Game.RightIsDown)
					{
						frameToPlay = 3;
					}
					if (Game.UpIsDown)
					{
						Player.Move(2);
					}
					if (Game.DownIsDown)
					{
						Player.Move(1);
					}
					if (Game.LeftIsDown)
					{
						Player.Move(4);
					}
					if (Game.RightIsDown)
					{
						Player.Move(3);
					}
					if (frameToPlay != 0)
					{
						Player.MoveFrame(frameToPlay);
					}
					
					Countdown--;
				}
				else
				{	
					if (Player.body != null)
					{
						Player.body.gotoAndStop(1);
					}
					if (Game.CIsPressed)
					{
						//parent is Game movieclip
						goToMenu();
					}
				}
			}
			else
			{
				if (!MakingDecion)
				{
					if (Game.ZIsPressed)
					{
						RemoveMessage = true;
					}
				}
			}
		}
		
		private function goToMenu():void
		{
			if (parent != null)
			{
				var theGame:Game = (parent as Game);
				theGame.SetOverworldInfo(OverworldBackgroundName, Player.x, Player.y);
				theGame.dispatchEvent(new Event(Game.EventMainMenu));
			}
		}
		
		private function goToBattle():void
		{
			if (parent != null)
			{
				var theGame:Game = (parent as Game);
				theGame.SetOverworldInfo(OverworldBackgroundName, Player.x, Player.y);
				theGame.dispatchEvent(new Event(Game.EventBattle));
			}
		}
		
		private function goToMainMenu():void
		{
			if (parent != null)
			{
				var theGame:Game = (parent as Game);
				theGame.dispatchEvent(new Event(Game.EventStartingMenu));
			}
		}
		
		private function removeChildren():void
		{
			if (numChildren > 0)
			{
				removeChildAt(0);
				removeChildren();
			}
		}
		
		public function AddContent(instanceName:String, playerX:int, playerY:int):void
		{
			var oldAreaName = StringHelper.Empty;
			if (OverworldBackground != null)
			{
				oldAreaName = OverworldBackground.AreaName;
			}
			
			removeChildren();
			
			Countdown = Constants.Random(0, 200);
			
			var tempPlayer:OverworldPlayer = new OverworldPlayer(1);
			tempPlayer.name = PlayerClipName;
			tempPlayer.x = playerX;
			tempPlayer.y = playerY;
			
			Player = tempPlayer;

			var copyBkg:Class = getDefinitionByName(instanceName) as Class;
			OverworldBackgroundName = instanceName;
			OverworldBackground = (new copyBkg);
			
			var tempFader:Fader = new Fader();
			FaderClip = tempFader;
			
			addChild(OverworldBackground);
			addChild(Player);
			
			if (!StringHelper.IsNullOrEmpty(oldAreaName) &&  oldAreaName != OverworldBackground.AreaName)
			{
				addChild(new LocationText(OverworldBackground.AreaName));
			}
			
			addChild(FaderClip);
		}
		
		public function DeleteMessage()
		{
			if (StringHelper.IsNullOrEmpty(MessageQueue))
			{
				actuallyDeleteMessage();
			}
			else
			{
				RenderMessage(MessageQueue);
			}
		}
		
		private function actuallyDeleteMessage():void
		{
			var messageBox:MovieClip = getChildByName(MessageClipName) as MovieClip;
			if (messageBox != null)
			{
				removeChild(messageBox);
			}
			MessageIsShown = false;
			RemoveMessage = false;
			MakingDecion = false;
		}
		
		public function DisplayMessage(variableToGet:String):void
		{
			if (!MessageIsShown)
			{
				Player.body.gotoAndStop(1);
				var tempArray:Array = variableToGet.split(Constants.MessageSplitCharacter);
				var index:int = tempArray[1];
				var messageText:String = OverworldBackground.GetMessage(index);
				RenderMessage(messageText);
			}
		}
		
		public function RenderMessage(messageText:String)
		{
			actuallyDeleteMessage();
			
			var tempArray:Array = messageText.split(Constants.NewlineCharacter);
			messageText = tempArray[0];
			
			MessageQueue = StringHelper.Empty;
			for (var i:int = 1; i < tempArray.length; i++)
			{
				if (i + 1 == tempArray.length)
				{
					MessageQueue += tempArray[i];
				}
				else
				{
					MessageQueue += (tempArray[i] + Constants.NewlineCharacter);
				}
			}
			
			tempArray = messageText.split(Constants.PortraitSplitCharacter);
			var portraitNum:int = tempArray[0];
			messageText = tempArray[1];
			
			tempArray = messageText.split(Constants.CommandSplitCharacter);
			messageText = tempArray[0];
			CommandName = tempArray[1];
			
			if (!StringHelper.IsNullOrEmpty(CommandName))
			{
				if (CommandName.search(Constants.Command_Movement) >= 0)
				{
					var backgroundIndex:int = tempArray[2];
					var commandNumber:int = tempArray[3];
					var nameString:String = ("person_" + backgroundIndex + "_" + portraitNum);
					var tempPerson:Person = OverworldBackground.getChildByName(nameString) as Person;
					tempPerson.MovementCommand(commandNumber);
				}
			}
			if (CommandName == Constants.Command_Quest)
			{
				increaseQuest();
			}
			if (!StringHelper.IsNullOrEmpty(CommandName) && 
				(CommandName == Constants.Command_SaveGame || CommandName == Constants.Command_Rest 
				 || CommandName == Constants.Command_ContinuePlaying))
			{
				MakingDecion = true;
			}
			else
			{
				MakingDecion = false;
			}

			var tempMessageBox:MessageBox = new MessageBox(messageText, MakingDecion, portraitNum)
			tempMessageBox.name = MessageClipName;
			addChild(tempMessageBox);
			MessageIsShown = true;
		}
		
		private function increaseQuest():void
		{
			var theGame:Game = (this.parent as Game);
			var tempQuest:int = theGame.MainInfo.Quest;
			tempQuest++;
			
			theGame.MainInfo.Quest = tempQuest;
			OverworldBackground.Quest = tempQuest;
		}
		
		private function saveGame():void
		{
			var theGame:Game = (this.parent as Game);
			theGame.SetOverworldInfo(OverworldBackgroundName, Player.x, Player.y);
			SaveGameManager.SaveGame(theGame);
		}
		
		private function playFader(waitTime:int, color:Number, speed:Number):void
		{
			FaderClip.PlayFader(waitTime, color, speed);
		}
		
		private function rest():void
		{
			var theGame:Game = (this.parent as Game);
			theGame.MainInfo.FullHeal();
			playFader(0, Number("0x000000"), .2);
		}
		
		public function YesSelected():void
		{
			switch(CommandName)
			{
				case Constants.Command_SaveGame:
					saveGame();
					RenderMessage(Constants.SaveGameSucessDialouge + Constants.NewlineCharacter + Constants.ContinuePlayingDialogue);
				break;
				case Constants.Command_ContinuePlaying:
					DeleteMessage();
				break;
				case Constants.Command_Rest:
					DeleteMessage();
					rest();
				break;
			}
		}
		public function NoSelected():void
		{
			switch(CommandName)
			{
				case Constants.Command_SaveGame:
					RenderMessage(Constants.ContinuePlayingDialogue);
				break;
				case Constants.Command_ContinuePlaying:
					goToMainMenu();
				break;
				case Constants.Command_Rest:
					DeleteMessage();
				break;
			}
		}
	}
}